﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Comm.DBHelper;
using System.Reflection;
using Comm.PublicClass;
using System.Windows.Forms;
using Comm.MultiLan;

namespace WsProtocol.IEC62056MDL
{
    [Serializable]
    public class HHULog
    {
        private static string ptltable = "T_HHULog";
        public static List<HHULog> AllHHULoglst = new List<HHULog>();  //HHU抄读数据详情表

        public string MeterNumber { get; set; }  //表号
        public string ExecutionTime { get; set; } //操作时间
        public string ProGramName { get; set; }   //方案名
        public string MeterAddress { get; set; }  //表地址
        public string OBISName { get; set; }  //数据名
        public string OBISValue { get; set; }  //OBIS
        public string RWFlag { get; set; }  //读写标志
        public string SetData { get; set; }  //参数
        public string ReadData { get; set; }  //抄读值
        public string UserName { get; set; }  //操作员名
        public string Result { get; set; }  //操作结果
        public int Seq { get; set; }


        public HHULog()
        {
            MeterNumber = "";
            ExecutionTime = "";
            ProGramName = "";
            MeterAddress = "";
            OBISName = "";
            OBISValue = "";
            RWFlag = "";
            SetData = "";
            ReadData = "";
            UserName = "";
            Result = "";
        }

        public static void StartTrans()
        {
            PtlConsts.easyDb.BeginTrans();
        }

        public static void RollbackTrans()
        {
            PtlConsts.easyDb.RollbackTrans();
        }

        public static void CommitTrans()
        {
            PtlConsts.easyDb.CommitTrans();
        }


        private static List<HHULog> TableToList(DataTable dt)
        {
            List<HHULog> list = new List<HHULog>();
            foreach (DataRow dr in dt.Rows)
            {
                HHULog m = GetModelByDataRow(dr);
                
                list.Add(m);
            }
            return list;
        }


        /// <summary>
        /// 数据行转对象
        /// </summary>
        /// <param name="dr"></param>
        /// <returns></returns>
        public static HHULog GetModelByDataRow(DataRow dr)
        {
            HHULog m = new HHULog();
            m.MeterNumber = dr["MeterNumber"].ToString();
            m.ExecutionTime = dr["ExecutionTime"].ToString();
            m.ProGramName = dr["ProGramName"].ToString();
            m.MeterAddress = dr["MeterAddress"].ToString();
            m.OBISName = dr["OBISName"].ToString();
            m.OBISValue = dr["OBISValue"].ToString();
            m.RWFlag = dr["RWFlag"].ToString();
            m.SetData = dr["SetData"].ToString();
            m.ReadData = dr["ReadData"].ToString();
            m.UserName = dr["UserName"].ToString();
            m.Result = dr["Result"].ToString();
            
            return m;
        }
       

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public static List<HHULog> GetAllHHULog()
        {            
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            PtlConsts.easyDb.SetOrder("Seq");
            AllHHULoglst = TableToList(PtlConsts.easyDb.Select());
            return AllHHULoglst;
        }

        /// <summary>
        /// 根据表号和时间获取详细数据
        /// </summary>
        /// <param name="meterno"></param>
        /// <param name="atime"></param>
        /// <returns></returns>
        public static List<HHULog> GetHHULogByMeter(string meterno, string atime)
        {
            if (AllHHULoglst.Count == 0)
                GetAllHHULog();  //获取所有数据

            List<HHULog> alst = new List<HHULog>();
            for (int i = 0; i < AllHHULoglst.Count; i++)
            {
                if (AllHHULoglst[i].MeterNumber == meterno && AllHHULoglst[i].ExecutionTime == atime)
                    alst.Add(AllHHULoglst[i]);
            }
            return alst;
        }


        /// <summary>
        /// 根据表号和时间删除数据
        /// </summary>
        /// <param name="meterno"></param>
        /// <param name="atime"></param>
        /// <returns></returns>
        public static int DelHHULogByMeter(string meterno, string atime, ref string errinfo)
        {
            PtlConsts.easyDb.Clear();
            PtlConsts.easyDb.TableName = ptltable;
            if (!string.IsNullOrEmpty(meterno))
                PtlConsts.easyDb.SetWhereParameter(true, "MeterNumber", "=", meterno, EasyDataType.String);
            if (!string.IsNullOrEmpty(atime))
                PtlConsts.easyDb.SetWhereParameter(true, "ExecutionTime", "=", atime, EasyDataType.String);
            int rs = PtlConsts.easyDb.Delete(ref errinfo);

            for (int i = AllHHULoglst.Count - 1; i >= 0; i--)
            {
                bool check = true;
                if (!string.IsNullOrEmpty(meterno))
                    check = check && AllHHULoglst[i].MeterNumber == meterno;
                if (!string.IsNullOrEmpty(atime))
                    check = check && AllHHULoglst[i].ExecutionTime == atime;
                if (check)
                    AllHHULoglst.RemoveAt(i);
            }
            return rs;
        }

    }
}
